package common.wx;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class MessageUtil {

	/**
	 * 返回消息类型：文本
	 */
	public static final String RESP_MESSAGE_TYPE_TEXT = "text";

	/**
	 * 返回消息类型：音乐
	 */
	public static final String RESP_MESSAGE_TYPE_MUSIC = "music";

	/**
	 * 返回消息类型：图文
	 */
	public static final String RESP_MESSAGE_TYPE_NEWS = "news";

	/**
	 * 请求消息类型：文本
	 */
	public static final String REQ_MESSAGE_TYPE_TEXT = "text";

	/**
	 * 请求消息类型：图片
	 */
	public static final String REQ_MESSAGE_TYPE_IMAGE = "image";

	/**
	 * 请求消息类型：链接
	 */
	public static final String REQ_MESSAGE_TYPE_LINK = "link";

	/**
	 * 请求消息类型：地理位置
	 */
	public static final String REQ_MESSAGE_TYPE_LOCATION = "location";

	/**
	 * 请求消息类型：音频
	 */
	public static final String REQ_MESSAGE_TYPE_VOICE = "voice";

	/**
	 * 请求消息类型：推送
	 */
	public static final String REQ_MESSAGE_TYPE_EVENT = "event";

	/**
	 * 事件类型：subscribe(订阅)
	 */
	public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";

	/**
	 * 事件类型：unsubscribe(取消订阅)
	 */
	public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";

	/**
	 * 事件类型：CLICK(自定义菜单点击事件)
	 */
	public static final String EVENT_TYPE_CLICK = "CLICK";

	/**
	 * 解析微信发来的请求（XML）
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> parseXml(HttpServletRequest request) {
		InputStream inputStream = null;
		try {
			inputStream = request.getInputStream();
			SAXReader reader = new SAXReader();
			Document document = reader.read(inputStream);
			return parseXml(document);
		} catch (Exception e) {
		} finally {
			if (null != inputStream)
				try {
					inputStream.close();
				} catch (Exception e2) {
				}
		}
		return null;
	}

	/**
	 * 解析微信发来的请求（XML）
	 * 
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> parseXml(String xml) {
		try {
			Document document = DocumentHelper.parseText(xml);
			return parseXml(document);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * map生成xml
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String mapToXml(Map<String, String> map) {
		List<String> keys = new ArrayList<String>(map.keySet());
		String xml = "<xml>";
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = map.get(key);
			xml += "<" + key + ">" + value + "</" + key + ">";
		}
		xml += "</xml>";
		return xml;
	}

	@SuppressWarnings("unchecked")
	private static Map<String, String> parseXml(Document document) {
		Map<String, String> map = new HashMap<String, String>();
		Element root = document.getRootElement();
		List<Element> elementList = root.elements();
		System.out.println("xmlMsg : start开始");
		for (Element e : elementList) {
			map.put(e.getName(), e.getText());
			System.out.print("{ " + e.getName() + " , " + e.getText() + " }");
		}
		System.out.println();
		System.out.println("xmlMsg : end结束");
		return map;
	}
}
